// pages/report/search_res/searchc_res.js
const api=require('../../utils/request')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareModel:{
        title:'',
        path:'pages/test/test', 
        fromButton:'one'
      },  
    tabNavActive:0,
    tabNav:["全部"],
    FX:false,
    pageNum:1,
    pageSize:10,
    total: 0,
    pages:0,
    recommendData:[
    
    ],
    searchInput:'',
    industryId:'',
    noData: false,
    gdLoading: true,
    posterId:'',
    name:''
  },
    //分享
    fxWrap(e){
        console.log('123',e);
        this.setData({
          FX:true,
          posterId: e.currentTarget.dataset.id,
          name: e.currentTarget.dataset.name
        })
      },
//取消
cancleBack(){
  wx.navigateBack({
    delta:1
  })
},
fxCancel(){
  this.setData({
    FX:false,
  });
},
tabNavClick(e){  
   this.setData({
    tabNavActive:e.currentTarget.dataset.id
  });
},

search(e){
    console.log('ww',e)
    this.setData({
        searchInput: e.detail.value
    })
},
changeData(){
    if(this.data.pages>this.data.pageNum){
       this.setData({
           pageNum: this.data.pageNum+1
       })
       this.loadData(2)
   }else{
      this.setData({
        gdLoading: false,
        noData: true
      })
   }
  },

loadData(type){
    let that=this
    let param={industryId:this.data.industryId,pageNum:this.data.pageNum,pageSize: this.data.pageSize}
    api.post('weChatMini/reportList',param).then(res=>{
        if(res.code===200){
            res.data.records.forEach(item=>{
                item.isSelected=true
            })
            if(type===2){
                let data=that.data.recommendData.concat(res.data.records)
                that.setData({
                    recommendData:data,
                    pages: res.data.pages,
                    
                })    
              }else{
                    that.setData({
                        recommendData:res.data.records,
                        pages: res.data.pages
                    })
                }
          
        }
    }) 
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('options',options)
   
    this.setData({
        industryId:options.industryId
    })
    this.loadData(1)
    this.findTagIndustry()
  },
  findTagIndustry(){
      api.get('weChatMini/findTagIndustry/'+this.data.industryId).then(res=>{
          if(res.code===200){
            wx.setNavigationBarTitle({
                title: res.data
            })
          }
      })
  },
  zan(e){
    let id=e.currentTarget.id
    let index= e.currentTarget.dataset.index 
    let data=this.data.recommendData
    let that=this
    let fasg=data[index].isSelected
    if(data[index].isSelected){
        data[index].browseNum=data[index].browseNum+1
    }else{
        data[index].browseNum=data[index].browseNum-1 
    }
    data[index].isSelected=!data[index].isSelected
    api.post('weChatMini/saveRecommend',{id: id ,isSelected: fasg}).then(res=>{
        if(res.code===200){
            that.setData({
                recommendData: data
            })
        }
    })
    
  },
  changePdf(e){
       
    let id=e.currentTarget.dataset.id
    wx.redirectTo({  url: '/pages/report/pdf/pdf?id='+id})

   
    
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
       
    if(this.data.pages>this.data.pageNum){
        this.setData({
            pageNum: this.data.pageNum+1
        })
        this.loadData(2)
    }else{
       this.setData({
         gdLoading: false,
         noData: true
       })
    }
  },
  copy(data){
    let reportId= this.data.posterId 
    wx.setClipboardData({
      data: 'https://www.xinyanbao.cn/#/pdf/?id='+reportId,
      success:function(res){ 
      }
    })
 },
   //保存海报
   save_report(){
    this.setData({
        FX: false
    })
  wx.navigateTo({
    url: '../report/save_report/save_report?posterId='+this.data.posterId,
  })
},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    console.log('res',res)
    if(res.from==='button'){
        this.setData({
            FX: false
        })
       return {
        title: this.data.name,
        path:'/pages/report/pdf/pdf?id='+this.data.posterId
      }
    }
 
}
})